let descr = $('.theme-descr');

for (let i = 0; i < descr.length; i++) {
    if (descr[i].offsetHeight > 100) {
        $($('.theme-btn')[i]).addClass('theme-btn-active');
        $(descr[i]).addClass('theme-descr-close');
        $('.theme-name'[i]).addClass('theme-name-close');
    }
}

$('.theme-btn').on('click', function () {
    let block = this.parentNode.parentNode;
    let theme_name = block.childNodes[3].childNodes[1];
    let theme_descr = block.childNodes[3].childNodes[3];

    if ($(theme_descr).hasClass('theme-descr-open')) {

        $(theme_name).addClass('theme-name-close');
        $(theme_name).removeClass('theme-name-open');

        $(theme_descr).addClass('theme-descr-close');
        $(theme_descr).removeClass('theme-descr-open');

        $(this).html('Подробнее');

    } else if ($(theme_descr).hasClass('theme-descr-close')) {

        $(theme_name).addClass('theme-name-open');
        $(theme_name).removeClass('theme-name-close');

        $(theme_descr).addClass('theme-descr-open');
        $(theme_descr).removeClass('theme-descr-close');

        $(this).html('Скрыть');
    }
});

function new_theme_open() {
    $('.new-theme-form').css({ 'display': 'flex' });
    $('.back-form').css({ 'display': 'block' });
}

function new_theme_exit() {
    $('.new-theme-form').css({ 'display': 'none' });
    $('.back-form').css({ 'display': 'none' });
    $('.new-theme-title').html('Создание новой темы');
    $('.btn-theme-create').html('Создать');
    $('.form-input').html('');
    $('.search-block-list').html('');
    $('.search-block-input').val('');
    $('.new-theme-input').val('');
    $('.new-theme-textarea').val('');
    $('.new-theme-textarea-score').html('0/10000');
    $('.theme-id').val('0');
}

// Редактирование

$('.edit').on('click', function () {
    let block = this.parentNode;
    let subs = block.childNodes[1].innerText;
    let subs_arr = [];

    $('.search-block-hidden-input').val(subs);

    if (subs.indexOf(',') > -1) {
        subs_arr = subs.split(',');
    } else {
        subs_arr.push(subs);
    }

    for (let i = 0; i < subs_arr.length; i++) {
        let li = document.createElement('li');
        let ul = $('.search-block-list');
        let div = document.createElement('div');

        li.innerHTML = subs_arr[i];
        li.className = 'search-block-item';
        div.className = 'search-block-div';
        ul.prepend(li);
        li.append(div);
        subject_remove();
    }
    let title = block.childNodes[3].childNodes[1].innerText;
    let descr = block.childNodes[3].childNodes[3].innerText;
    let theme_id = $(block).attr('id');
    let subject_input = block.childNodes[1].innerText;

    new_theme_open();
    $('.new-theme-title').html('Редактирование темы');
    $('.new-theme-create').html('Сохранить');

    $('.new-theme-input').val(title);
    $('.new-theme-textarea').val(descr);
    $('.theme-id').val(theme_id);
    $('.subject-input').val(subject_input);
});

// цвета

$("ul li.list-li").click(function () {
    $("#subject-filter").val($(this).text());
});

// фильтры

function filters_open(btn) {
    let div = btn.parentNode;
    let content = div.querySelector('.filters-content');
    let svg = btn.querySelector('.filters-svg');
    if ($(content).hasClass('list-hidden')) {
        $(content).removeClass('list-hidden');
        $(svg).css({'transform' : 'rotate(0deg)'});
        if (content.scrollHeight > 250) {
            $(content).css({'height' : '250px'});
        } else {
            $(content).css({'height' : content.scrollHeight + 'px'});
        }
    } else {
        $(content).addClass('list-hidden');
        $(svg).css({'transform' : 'rotate(45deg)'});
    }
}

function filters_search(input) {
    let text = input.value.trim();
    let content = input.parentNode.parentNode
    let list = content.querySelectorAll('.filters-item');
    for (let i = 1; i < list.length; i++) {
        let item_text = list[i].querySelector('.filters-text').textContent;
        if (item_text.toLowerCase().match(text.toLowerCase()) || text == '') {
            $(list[i]).removeClass('hidden');
        } else {
            $(list[i]).addClass('hidden');
        }
    }
    if (content.scrollHeight > 250) {
            $(content).css({'height' : '250px'});
        } else {
            $(content).css({'height' : content.scrollHeight + 'px'});
        }
}

function filters_clear(btn) {
    let input = btn.parentNode.querySelector('.filters-input');
    input.value = '';
    filters_search(input);
}


function textareaScore(el) {
    let span = document.querySelector('.new-theme-textarea-score');
    let score = el.value.length;
    span.textContent = score + '/10000';
}